import { agentData, savedAgent } from '../../../js/netCase/agent/agentData.js'
import ecfsConfig from '../../../config/ecfs.config.js'
import { merge } from '../../../utils/util.js'
import locale from '../../../config/locale/zh-CN/netCase/party.js'
import * as agentBussiness from '../../../js/netCase/agent/agentBussiness.js'
import * as partyApi from '../../../api/party.js'
// pages/netCase/agent/agent.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    agentData,
    savedAgent,
    locale,
    agentTypeIndex: 0,
    CertifIndex: 0,
    saveAgent: {},//向后台传数据的对象
    formIsFrom: 4,//来自于上一页面传值
    accusers: [{
      caseId: '22',
      partyId: '111',
      partyName: '2222'}],
    isDisabled: false,//是否可编辑  true-不可编辑  false-可编辑
  },
  /**
   * 点击取消按钮
   */
  formReset: function (e) {

  },
  /**
 * 提示
 */
  showTips(msg) {
    wx.showToast({
      title: msg,
      icon: 'none',
      duration: 1000
    })
  },
  /**
   * 保存代理人
   */
  saveAgent: function (e) {
    let saveAgent = merge({}, this.data.savedAgent), flag = true
    if (saveAgent.agentType === 1) {//律师
      if (saveAgent.licenseNumber === '') {
        this.showTips('请输入执业证号')
        flag = false
      }
    }
    if (saveAgent.name === '') {
      this.showTips('请输入姓名')
      flag = false
    } else if (saveAgent.certificateNumber.length < 18) {
      this.showTips('请输入正确身份证信息')
      flag = false
    } else if (saveAgent.phoneNumber.length < 11) {
      this.showTips('请输入正确手机号码')
      flag = false
    } else if (saveAgent.partyIds.length == 0) {
      this.showTips('请选择被代理人姓名')
      flag = false
    }
    if (flag) {
      this.data.saveAgent = merge({}, saveAgent)
      agentBussiness.handleSaveAgent(this)
    }
  },
  /**
   * 删除代理人
   */
  deleteAgent: function (e) {
    let content = {
      caseId: this.data.savedAgent.caseId,
      flag: 2,
      id: this.data.savedAgent.id
    }
    if (content.caseId != '' && content.id != '') {
      agentBussiness.handleDeleteAgent(this, content)
    } else {
      this.showTips('信息不全删除失败')
    }
  },
  /**
   * 选择被代理人
   */
  checkboxChange: function (e) {
    let checked = merge({}, e.detail.value), non = []
    for (let index in checked) {
      let item = { partyId: checked[index] }
      non.push(item)
    }
    this.setData({
      'savedAgent.partyIds': non
    })
  },
  /**
 * 获取手机号码
 */
  bindPhoneNum: function (e) {
    this.setData({
      'savedAgent.phoneNumber': e.detail.value
    })
  },
  /**
   * 绑定执业证号
   */
  bindLicenseNum: function (e) {
    this.setData({
      'savedAgent.licenseNumber': e.detail.value
    })
  },
  /**
  * 获取证件号码
  */
  bindCertifyNum: function (e) {
    this.setData({
      'savedAgent.certificateNumber': e.detail.value
    })
  },
  /**
   * 获取证件类型
   */
  bindCertificateType: function (e) {
    this.setData({
      CertifIndex: e.detail.value
    })
    for (var index in this.data.agentData.certificateTypeList) {
      if (e.detail.value == index) {
        this.setData({
          'savedAgent.certificateType': this.data.agentData.certificateTypeList[index].id,
          'savedAgent.certificateTypeName': this.data.agentData.certificateTypeList[index].name
        })
      }
    }
  },
  /**
   * 代理人姓名
   */
  bindName: function (e) {
    this.setData({
      'savedAgent.name': e.detail.value
    })
  },
  /**
   * 选择代理人类型
   */
  changeAgentType: function (e) {
    for (var index in this.data.agentData.agentTypeData) {
      if (index === e.detail.value) {
        this.setData({
          agentTypeIndex: e.detail.value,
          'savedAgent.agentType': Number(this.data.agentData.agentTypeData[index].id),
          'savedAgent.agentTypeName': this.data.agentData.agentTypeData[index].name
        })
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    取url传入数据
    this.setData({
      formIsFrom: Number(options.formIsFrom)
    })
    if (this.data.formIsFrom === 5) {
      this.setData({
        isDisabled: true
      })
    } else {
      this.setData({
        isDisabled: false
      })
    }
    if (this.data.formIsFrom === 5) {
      const config = {
        url: ecfsConfig.IS_PRODUCTION ? (ecfsConfig.SERVER_DOMAIN + ecfsConfig.SELECT_AGENT_URL) : (ecfsConfig.SERVER_DOMAIN + ecfsConfig.RAP_URL_PREFIX + ecfsConfig.SELECT_AGENT_URL),
        data: {
          id: options.agentId
        },
      }
      partyApi.selectAgent(config).then(res => {
        let data = res.data
        if (data.code === 200) {
          this.setData({
            savedAgent: data.body
          })
        } else {
          console.log(data.code)
          console.log('请求失败')
        }
      }).catch(error => {
        console.log(error)
      })
    }
    从缓存中获取原告信息
    wx.getStorage({
      key: 'accusers',
      success: function (res) {
        // console.log(res)
        this.setData({
          accusers: res.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})